//
//  RaiseManageViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/7/10.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "RaiseManageViewController.h"
#import "RaiseRecordTableViewCell.h"
#import "RaiseItemTableViewCell.h"
#import "ItemInfo.h"
#import "AppDelegate.h"
#import "KJSRequest.h"
#import <MJRefresh.h>
#import "TransverseSelectView.h"

@interface RaiseManageViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,assign) int page;
@property (nonatomic,assign) int rows;
@property (nonatomic,assign) int itemStatus;
@property (nonatomic,strong) NSMutableArray *records;
@property (nonatomic,strong) NSMutableArray *items;
@property (weak, nonatomic) IBOutlet TransverseSelectView *transverseView;

@end

@implementation RaiseManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"融资管理"];
    self.recordTableV.dataSource = self;
    self.recordTableV.delegate = self;
    
    self.transverseView.titleArray = @[@"融资记录",@"募集期中的项目",@"募集成功的项目",@"募集终止的项目"];
    self.transverseView.selectAction = ^(NSInteger selectLocation){
        [self configeration];
        NSLog(@"选择 %ld",(long)selectLocation);
        switch (selectLocation) {
            case 0:
                self.cellType = raiseRecord;
                [self GetRaiseRecord];
                break;
            case 1:
                self.cellType = raiseRaisingItem;
                self.itemStatus = 7;
                [self GetRaiseItem];
                break;
            case 2:
                self.cellType = raiseSuccessItem;
                self.itemStatus = 9;
                [self GetRaiseItem];
                break;
            case 3:
                self.cellType = raiseFailItem;
                self.itemStatus = 10;
                [self GetRaiseItem];
                break;
            default:
                break;
        }
    };

    
    [self configeration];
    [self configurateTableView];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configeration];
    [self GetRaiseRecord];
}
#pragma mark - configeration
- (void)configeration
{
    self.rows = 3;
    self.page = 1;
    self.items = [[NSMutableArray alloc] init];
    self.records = [[NSMutableArray alloc] init];
    self.cellNum = 0;
}
- (void)configurateTableView {
    self.recordTableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [footer setTitle:@"上拉加载更多数据" forState:MJRefreshStateIdle];
    [footer setTitle:@"用力拉！~" forState:MJRefreshStatePulling];
    [footer setTitle:@"更新数据中~" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据" forState:MJRefreshStateNoMoreData];
    
    self.recordTableV.mj_footer = footer;
    [self.recordTableV registerNib:[UINib nibWithNibName:@"RaiseRecordView" bundle:nil] forCellReuseIdentifier:@"RaiseRecordTableViewCell"];
    [self.recordTableV registerNib:[UINib nibWithNibName:@"RaiseItemView" bundle:nil] forCellReuseIdentifier:@"RaiseItemTableViewCell"];

}

#pragma mark - custom method

- (void)GetRaiseRecord
{
    if (CurentDelegate.account) {
        [KJSRequest GetRaiseRecordWithPartyId:CurentDelegate.account.partyId page:self.page rows:self.rows ret:^(RaiseRecordModel *model) {
            NSLog(@"成功了");
            [self.records addObjectsFromArray:model.raiserecords];
            [(MJRefreshAutoNormalFooter *)self.recordTableV.mj_footer setTitle:([model.raiserecords count] < ROW_OF_EACH_PAGE)? @"没有更多数据了" :@"上拉加载更多数据" forState:MJRefreshStateIdle];
            self.cellNum = [self.records count];
            [self endRefresh];
            [self.recordTableV reloadData];
        }];
        
    }
}
- (void)GetRaiseItem
{
    if (CurentDelegate.account) {
        [KJSRequest GetRaiseItemWithPartyId:CurentDelegate.account.partyId page:self.page rows:self.rows itemStatus:self.itemStatus ret:^(RaiseItemModel *model) {
            [self.items addObjectsFromArray:model.myraises];
            [(MJRefreshAutoNormalFooter *)self.recordTableV.mj_footer setTitle:([model.myraises count] < ROW_OF_EACH_PAGE)? @"没有更多数据了" :@"上拉加载更多数据" forState:MJRefreshStateIdle];
            self.cellNum = [self.items count];
            [self endRefresh];
            [self.recordTableV reloadData];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellNum;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld",self.cellType);
    switch (self.cellType) {
        case raiseRecord:
        {
            RaiseRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RaiseRecordTableViewCell"];
            if ([self.records count] > indexPath.row) {
                cell.record = self.records[indexPath.row];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            return cell;
        }
            break;
        case raiseRaisingItem:
        {
            RaiseItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RaiseItemTableViewCell"];
            if ([self.items count] > indexPath.row) {
                cell.item = self.items[indexPath.row];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            return cell;
        }
            break;
        case raiseSuccessItem:
        {
            RaiseItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RaiseItemTableViewCell"];
            if ([self.items count] > indexPath.row) {
                cell.item = self.items[indexPath.row];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            return cell;
        }
            break;
        case raiseFailItem:
        {
            RaiseItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RaiseItemTableViewCell"];
            if ([self.items count] > indexPath.row) {
                cell.item = self.items[indexPath.row];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            return cell;
        }
            break;
        default:
        {
            RaiseRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RaiseRecordTableViewCell"];
            return cell;
        }
            break;
    }
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

#pragma mark - refresh method
- (void)headerRefresh {
    [self configeration];
    switch (self.cellType) {
        case raiseRecord:
            [self GetRaiseRecord];
            break;
        case raiseRaisingItem:
            [self GetRaiseItem];
            break;
        case raiseSuccessItem:
            [self GetRaiseItem];
            break;
        case raiseFailItem:
            [self GetRaiseItem];
            break;
        default:
            [self GetRaiseRecord];
            break;
    }
}

- (void)footerRefresh {
    switch (self.cellType) {
        case raiseRecord:
            self.page += 1;
            [self GetRaiseRecord];
            break;
        case raiseRaisingItem:
            self.page += 1;
            [self GetRaiseItem];
            break;
        case raiseSuccessItem:
            self.page += 1;
            [self GetRaiseItem];
            break;
        case raiseFailItem:
            self.page += 1;
            [self GetRaiseItem];
            break;
        default:
            self.page += 1;
            [self GetRaiseRecord];
            break;
    }
}

- (void)endRefresh {
    [self.recordTableV.mj_header endRefreshing];
    [self.recordTableV.mj_footer endRefreshing];
}
@end
